package al.xc.wxsession.config;

import al.xc.common.util.SingleUtils;
import al.xc.redis.IRedisConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Scope("singleton")
@Slf4j
public
class RedisConfigImpl implements IRedisConfig {

    // redis config
    @Value("${redis.urls}")
    private String redisUrls;
    @Value("${redis.timeout}")
    private Integer redisTimeout;
    @Value("${redis.password}")
    private String redisPassword;
    @Value("${redis.databaseIndex}")
    private Integer redisDatabaseIndex;

    @Value("${redis.poolConfig.maxTotal}")
    private Integer redisPoolConfigMaxTotal;
    @Value("${redis.poolConfig.MaxIdle}")
    private Integer redisPoolConfigMaxIdle;
    @Value("${redis.poolConfig.MaxWaitMillis}")
    private Long redisPoolConfigMaxWaitMillis;
    @Value("${redis.poolConfig.testOnBorrow}")
    private Boolean redisPoolConfigTestOnBorrow;
    @Value("${redis.poolConfig.testOnReturn}")
    private Boolean redisPoolConfigTestOnReturn;
    @Value("${redis.poolConfig.testWhileIdle}")
    private Boolean redisPoolConfigTestWhileIdle;
    @Value("${redis.poolConfig.timeBetweenEvictionRunsMillis}")
    private Integer timeBetweenEvictionRunsMillis;
    @Value("${redis.poolConfig.numTestsPerEvictionRun}")
    private Integer numTestsPerEvictionRun;
    @Value("${redis.poolConfig.minEvictableIdleTimeMillis}")
    private Integer minEvictableIdleTimeMillis;

    @PostConstruct
    private void initialize() {
        SingleUtils.instance(IRedisConfig.class, this);
    }

    @Override
    public String getRedisUrls() {
        return redisUrls;
    }

    public void setRedisUrls(String redisUrls) {
        this.redisUrls = redisUrls;
    }

    @Override
    public Integer getRedisTimeout() {
        return redisTimeout;
    }

    public void setRedisTimeout(Integer redisTimeout) {
        this.redisTimeout = redisTimeout;
    }

    @Override
    public String getRedisPassword() {
        return redisPassword;
    }

    public void setRedisPassword(String redisPassword) {
        this.redisPassword = redisPassword;
    }

    @Override
    public Integer getRedisDatabaseIndex() {
        return redisDatabaseIndex;
    }

    public void setRedisDatabaseIndex(Integer redisDatabaseIndex) {
        this.redisDatabaseIndex = redisDatabaseIndex;
    }

    @Override
    public Integer getRedisPoolConfigMaxTotal() {
        return redisPoolConfigMaxTotal;
    }

    public void setRedisPoolConfigMaxTotal(Integer redisPoolConfigMaxTotal) {
        this.redisPoolConfigMaxTotal = redisPoolConfigMaxTotal;
    }

    @Override
    public Integer getRedisPoolConfigMaxIdle() {
        return redisPoolConfigMaxIdle;
    }

    public void setRedisPoolConfigMaxIdle(Integer redisPoolConfigMaxIdle) {
        this.redisPoolConfigMaxIdle = redisPoolConfigMaxIdle;
    }

    @Override
    public Long getRedisPoolConfigMaxWaitMillis() {
        return redisPoolConfigMaxWaitMillis;
    }

    public void setRedisPoolConfigMaxWaitMillis(Long redisPoolConfigMaxWaitMillis) {
        this.redisPoolConfigMaxWaitMillis = redisPoolConfigMaxWaitMillis;
    }

    @Override
    public Boolean getRedisPoolConfigTestOnBorrow() {
        return redisPoolConfigTestOnBorrow;
    }

    public void setRedisPoolConfigTestOnBorrow(Boolean redisPoolConfigTestOnBorrow) {
        this.redisPoolConfigTestOnBorrow = redisPoolConfigTestOnBorrow;
    }

    @Override
    public Boolean getRedisPoolConfigTestOnReturn() {
        return redisPoolConfigTestOnReturn;
    }

    public void setRedisPoolConfigTestOnReturn(Boolean redisPoolConfigTestOnReturn) {
        this.redisPoolConfigTestOnReturn = redisPoolConfigTestOnReturn;
    }

    @Override
    public Boolean getRedisPoolConfigTestWhileIdle() {
        return redisPoolConfigTestWhileIdle;
    }

    public void setRedisPoolConfigTestWhileIdle(Boolean redisPoolConfigTestWhileIdle) {
        this.redisPoolConfigTestWhileIdle = redisPoolConfigTestWhileIdle;
    }

    public Integer getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis;
    }

    public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    @Override
    public Integer getNumTestsPerEvictionRun() {
        return numTestsPerEvictionRun;
    }

    public void setNumTestsPerEvictionRun(Integer numTestsPerEvictionRun) {
        this.numTestsPerEvictionRun = numTestsPerEvictionRun;
    }

    @Override
    public Integer getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }

    public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    @Override
    public void showConfig() {
        log.info("-----------------------Redis-CONFIG INFO-----------------------");
        log.info("redisURL                       : " + redisUrls);
        log.info("redisTimeout                   : " + redisTimeout);
        log.info("redisPassword                  : " + redisPassword);
        log.info("redisDatabaseIndex             : " + redisDatabaseIndex);
        log.info("redisPoolConfigMaxTotal        : " + redisPoolConfigMaxTotal);
        log.info("redisPoolConfigMaxIdle         : " + redisPoolConfigMaxIdle);
        log.info("redisPoolConfigMaxWaitMillis   : " + redisPoolConfigMaxWaitMillis);
        log.info("redisPoolConfigTestOnBorrow    : " + redisPoolConfigTestOnBorrow);
        log.info("redisPoolConfigTestOnReturn    : " + redisPoolConfigTestOnReturn);
        log.info("redisPoolConfigTestWhileIdle   : " + redisPoolConfigTestWhileIdle);
        log.info("timeBetweenEvictionRunsMillis  : " + timeBetweenEvictionRunsMillis);
        log.info("numTestsPerEvictionRun         : " + numTestsPerEvictionRun);
        log.info("minEvictableIdleTimeMillis     : " + minEvictableIdleTimeMillis);
        log.info("-----------------------Redis-CONFIG INFO-----------------------");
    }
}
